<template>
  <div>
    <danger-detail-info :form="form"></danger-detail-info>
    <div class="block-box">
      <div class="block-box_item">
        <div class="block-box_title">指派整改人</div>
        <div>
          <div class="det_tab_title"></div>
          <eagle-radio-group
            v-model="radio"
            label="判定"
            name="radio"
            direction="horizontal"
          >
            <eagle-radio name="1" icon-size="18px" style="margin: 0 10px 10px 0"
              >指派</eagle-radio
            >
            <eagle-radio name="2" icon-size="18px" style="margin: 0 10px 10px 0"
              >退回隐患</eagle-radio
            >
          </eagle-radio-group>
          <div v-if="radio == 2">
            <van-field
              v-model="remarks"
              rows="3"
              autosize
              required
              label="退回说明"
              type="textarea"
              placeholder="请输入"
              show-word-limit
            />
          </div>
          <div v-else>
            <eagle-input
              :value="getCorrectUserText(editForm)"
              label="整改部门/人"
              placeholder="请选择"
              required
              readonly
              @click="openDeprtmentUserChooseDialog()"
            ></eagle-input>

            <eagle-input
              :value="editForm.Remarks"
              v-if="!isMe"
              label="说明"
              placeholder="请输入"
              type="textarea"
              required
              isInputText
            ></eagle-input>

            <div v-if="isMe">
              <eagle-radio-group
                v-model="editForm.CorrectiveMethod"
                label="整改方式"
                name="radio"
                direction="horizontal"
              >
                <eagle-radio name="1">立即整改</eagle-radio>
                <eagle-radio name="2">限期整改</eagle-radio>
              </eagle-radio-group>
              
              <eagle-input
                name="uploader"
                label="整改图片"
                v-if="editForm.CorrectiveMethod == '1'"
                required
              >
                <template v-if="editForm.CorrectiveMethod == '1'" #input>
                  <eagle-image-uploader :images.sync="editForm.CorrectiveAttachs"></eagle-image-uploader>
                </template>
              </eagle-input>
              <eagle-input
                v-if="editForm.CorrectiveMethod == '1'"
                v-model="editForm.CorrectiveDesc"
                required
                rows="3"
                autosize
                label="整改说明"
                type="textarea"
                placeholder="请输入整改说明"
                isInputText
              ></eagle-input>
              <eagle-date
                label="整改期限"
                required
                v-if="editForm.CorrectiveMethod == '2'"
                :date.sync="editForm.CorrectiveDate"
              ></eagle-date>
              <eagle-input
                v-if="editForm.CorrectiveMethod == '2'"
                v-model="editForm.CorrectiveMeasure"
                required
                rows="2"
                autosize
                label="整改措施"
                type="textarea"
                placeholder="请输入整改措施"
                isInputText
              ></eagle-input>
            </div>
          </div>
        </div>
      </div>
      <div class="btns mt-10 mb-10">
        <eagle-button class="submit-only" @click="submit">提交</eagle-button>
      </div>
      <!-- 部门，人 -->
      <deprtment-user-choose-dialog
        v-model="deprtmentUserChooseDialogShow"
        @callback="departmentUserChooseCallback"
      ></deprtment-user-choose-dialog>
      <!-- <van-popup
        v-model="deprtmentUserChooseDialogShow"
        closeable
        round
        position="bottom"
        :style="{ height: '80%' }"
      >
        <div class="popup-title">选择整改部门/人</div>
        <div class="popup-content">
          <DeprtmentUserChooseWindow
            @DepartmentCb="departmentUserChooseCallback"
          />
        </div>
      </van-popup> -->
    </div>
  </div>
</template>

<script>
import { getDate, dateAdd } from "@/utils/getDate";
import { deepClone } from "@/utils";
import DangerDetailInfo from "./dangerDetailInfo.vue";
import deprtmentUserChooseDialog from "@/views/components/department/departmentUserChooseDialog.vue";
export default {
  components: {
    deprtmentUserChooseDialog,
    DangerDetailInfo,
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      id: this.$route.query.id,
      border: false,
      active: "a",
      reason: false,
      expenses: false,
      showCalendar: false,
      correctiveDate: null,
      isMe: false,
      form: {},
      editForm: {},
      radio: "1",
      name: "",
      remarks: "",
      deprtmentUserChooseDialogShow: false,
    };
  },
  created() {
    this.geDetail();
  },
  methods: {
    // 获取详情
    geDetail() {
      this.$axios
      .get("/DangerCheckTaskDetail/GetDetail",{params:{ id: this.$route.query.id }})
      .then((res)=>{
        this.$set(this, "form", res.data);
        this.editForm = deepClone(res.data);
        this.editForm.CorrectiveDeptName = "";
        this.editForm.CorrectiveDeptCode = "";
        this.editForm.CorrectiveDate = getDate(
          this.editForm.CorrectiveDate || dateAdd("w", 1, new Date())
        );
        this.editForm.CorrectiveMethod = this.editForm.CorrectiveMethod || "2";
        this.correctiveDate = new Date(this.editForm.CorrectiveDate);
      })
    },
    openDeprtmentUserChooseDialog() {
      this.deprtmentUserChooseDialogShow = true;
    },
    // 选择部门人回调
    departmentUserChooseCallback(item, type) {
      if (item) {
        if (type === 0) {
          this.name = item.OrgName;
        } else {
          this.name = item.ChnName;
        }
        this.editForm.CorrectiveDeptName = item.OrgName;
        this.editForm.CorrectiveDeptCode = item.OrgCode;
        this.editForm.CorrectiveChnName = item.ChnName;
        this.editForm.CorrectiveUserName = item.UserName;
        this.isMe = this.$store.state.userName == this.editForm.CorrectiveUserName;
      }
      this.deprtmentUserChooseDialogShow = false;
    },
    getCorrectUserText(item) {
      if (item.CorrectiveDeptName || item.CorrectiveChnName) {
        if (item.CorrectiveChnName && item.CorrectiveDeptName) {
          return item.CorrectiveDeptName + "-" + item.CorrectiveChnName;
        } else if (item.CorrectiveChnName) {
          return item.CorrectiveChnName;
        } else {
          return item.CorrectiveDeptName;
        }
      }
      return "";
    },
    openCalendar() {
      this.showCalendar = true;
    },
    onCalendarCallback(date) {
      this.editForm.CorrectiveDate = getDate(date);
      this.correctiveDate = date;
      this.showCalendar = false;
    },
    submit() {
      // this.loading = true
      if (this.radio == 1) {
        let param = {
          ID: this.editForm.ID,
          CorrectiveDeptName: this.editForm.CorrectiveDeptName,
          CorrectiveDeptCode: this.editForm.CorrectiveDeptCode,
          CorrectiveChnName: this.editForm.CorrectiveChnName,
          CorrectiveUserName: this.editForm.CorrectiveUserName,
          CorrectiveMethod: this.editForm.CorrectiveMethod,
          CorrectiveDate: this.editForm.CorrectiveDate,
          CorrectiveMeasure: this.editForm.CorrectiveMeasure,
          CorrectiveDesc: this.editForm.CorrectiveDesc,
          Analysis: this.editForm.Analysis,
          Expenses: this.editForm.Expenses,
          Remarks: this.editForm.Remarks,
          CorrectiveAttachs: this.editForm.CorrectiveAttachs,
        };
        if (!param.CorrectiveDeptCode && !param.CorrectiveUserName) {
          this.$toast("请选择整改部门/人");
          return false;
        }
        if (this.isMe) {
          if (param.CorrectiveMethod == "1") {
            if (!param.CorrectiveAttachs) {
              this.$toast("请选择整改图片");
              return false;
            }
            if (!param.CorrectiveDesc) {
              this.$toast("请输入整改说明");
              return false;
            }
          } else {
            if (!param.CorrectiveDate) {
              this.$toast("请选择整改日期");
              return false;
            }
            if (!param.CorrectiveMeasure) {
              this.$toast("请输入整改措施");
              return false;
            }
          }
        }
        this.$axios
        .post("/DangerCheckTaskDetail/ConfirmAssign",param)
        .then((res)=>{
           this.$toast("提交成功");
          this.$router.goRefresh(-1);
        })
        .catch((err) => {
          console.log("err", err);
        });
      } else {
        let param = {
          ID: this.editForm.ID,
          Remarks: this.remarks,
        };
        if (!param.Remarks) {
          this.$toast("请输入退回说明");
          return false;
        }
        this.$axios
        .post("/DangerCheckTaskDetail/BackAssign",param)
        .then((res)=>{
           this.$toast("退回成功");
          this.$router.goRefresh(-1);
        })
        .catch((err) => {
          console.log("err", err);
        });
      }
    },
    // 一周
    getOneWeek() {
      let date = getDate(dateAdd("w", 1, new Date()));
      this.$set(this.editForm, "CorrectiveDate", date);
      this.correctiveDate = new Date(date);
    },
    // 两周
    getTwoWeek() {
      let date = getDate(dateAdd("w", 2, new Date()));
      this.$set(this.editForm, "CorrectiveDate", date);
      this.correctiveDate = new Date(date);
    },
    getOneMounth() {
      let date = getDate(dateAdd("m", 1, new Date()));
      this.$set(this.editForm, "CorrectiveDate", date);
      this.correctiveDate = new Date(date);
    },
  },
};
</script>
